package com.pine.manager.core;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.pine.common.beans.enums.IntegerCodeBaseEnum;
import lombok.NoArgsConstructor;

/**
 * 整数code枚举转换器
 *
 * @author pine manage
 * @since 2024-08-20
 */
@SuppressWarnings("all")
@NoArgsConstructor
public class IntergerCodeEnumConverter implements Converter<IntegerCodeBaseEnum> {

    public Class<?> supportJavaTypeKey() {
        return IntegerCodeBaseEnum.class;
    }

    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @SuppressWarnings("all")
    public WriteCellData<?> convertToExcelData(IntegerCodeBaseEnum value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new WriteCellData<>(value.getDesc());
    }

    @SuppressWarnings("all")
    public IntegerCodeBaseEnum convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        Class<? extends IntegerCodeBaseEnum> cls = (Class<? extends IntegerCodeBaseEnum>) contentProperty.getField().getType();
        if (cellData.getNumberValue() != null) {
            return IntegerCodeBaseEnum.convertDescToEnum(cellData.getNumberValue().toString(), cls);
        } else {
            return cellData.getStringValue() != null ? IntegerCodeBaseEnum.convertDescToEnum(cellData.getStringValue(), cls) : null;
        }
    }
}
